package Services

import(
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"gin/Databases"
	"gin/Common"
	"time"
)

type CustomTaskAddCgi struct {
	Common.CgiBase
}

func (cgi CustomTaskAddCgi)quertTask() (bool, int) {
	var cnt []int
	sql := "select count(1) from db_custom_task where Ftask_date=? and Femail=? and Fentity_name=? and Fcustom_word=?;"
	err := Databases.Db.Select(&cnt, sql, cgi.Input["task_date"], cgi.Input["email"], cgi.Input["entity_name"], cgi.Input["custom_word"])
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false,0
	}

    return true, cnt[0]
}

func (cgi CustomTaskAddCgi)addTask(class_id int) (bool) {
	fmt.Println("addTask, ", cgi.Input["task_date"], cgi.Input["entity_name"],
									cgi.Input["start_date"], cgi.Input["custom_word"], cgi.Input["email"])
	sql := "insert into db_custom_task (Ftask_date,Fclass,Fentity_name,Fstart_date,Fcustom_word,Femail,Fstatus,Fcreate_time,Fmodify_time) values (?,?,?,?,?,?,0,now(),now());"
	_,err := Databases.Db.Exec(sql, cgi.Input["task_date"], class_id, cgi.Input["entity_name"],
									cgi.Input["start_date"], cgi.Input["custom_word"], cgi.Input["email"])
	if err != nil {
		fmt.Println("err:", err)
		return false
	}
	return true
}

func (cgi CustomTaskAddCgi)updateTask() (bool) {
	sql := "update db_custom_task set Fstart_date=?,Fmodify_time=now() where Ftask_date=? and Femail=? and Fentity_name=? and Fcustom_word=?;"
	_,err := Databases.Db.Exec(sql, cgi.Input["start_date"],
									cgi.Input["task_date"], cgi.Input["email"], cgi.Input["entity_name"], cgi.Input["custom_word"])
	if err != nil {
		return false
	}
	return true
}

func (cgi CustomTaskAddCgi)quertEntity() (bool, int) {
	fmt.Println("quertEntity")
	var class_id []int
	sql := "select Fclass1 from db_entity_set_data where Fname=?;"
	err := Databases.Db.Select(&class_id, sql, cgi.Input["entity_name"])
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false,0
	}

	if len(class_id) == 0 {
		fmt.Println("exec failed, ", err)
		return false,0
	}

    return true, class_id[0]
}

func (cgi CustomTaskAddCgi)quertUserEntity() (bool) {
	fmt.Println("quertUserEntity")
	var entity_name []string
	sql := "select Fname from db_entity_suggest where Fname=? and Fuser=?;"
	err := Databases.Db.Select(&entity_name, sql, cgi.Input["entity_name"], cgi.Input["email"])
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false
	}

	if len(entity_name) == 0 {
		fmt.Println("exec failed, ", err)
		return false
	}

    return true
}


func (cgi *CustomTaskAddCgi)processParam(c *gin.Context) {
	time_now := time.Now()
	//cgi.Input["name"] = time_now.Format("2006-01-02")
	cgi.Input["task_date"] = time_now.Format("2006-01-02")
	cgi.Input["entity_name"] = c.PostForm("entity_name")
	start_date := c.DefaultPostForm("start_date", "")
	if start_date == "" {
		cgi.Input["start_date"] = time_now.AddDate(0, 0, -1 * 30).Format("2006-01-02")
	} else {
		temp,err := time.Parse("2006-01-02", start_date)
		if err != nil {
			cgi.Input["start_date"] = time_now.AddDate(0, 0, -1 * 30).Format("2006-01-02")
		} else {
			if temp.Before(time_now.AddDate(0, 0, -1 * 365)) || temp.After(time_now) {
				cgi.Input["start_date"] = time_now.AddDate(0, 0, -1 * 30).Format("2006-01-02")
			} else {
				cgi.Input["start_date"] = temp.Format("2006-01-02")
			}
		}
	}
	cgi.Input["custom_word"] = c.DefaultPostForm("custom_word", "")
	cgi.Input["email"] = c.PostForm("email")
}

// 实体集数据
func CustomTaskAdd(c *gin.Context) {
	var cgi CustomTaskAddCgi
	cgi.Init(c)
	cgi.processParam(c)

	entity_res, class_id := cgi.quertEntity()
	if (!entity_res) {
		entity_res = cgi.quertUserEntity();
	}
	if (!entity_res) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "实体集错误",
	        "data": "{}",
	    })
	    return
	}

	res, cnt := cgi.quertTask()
	if (!res) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "添加失败",
	        "data": "{}",
	    })
	    return
	}

    if (0 == cnt) {
    	res = cgi.addTask(class_id)
    } else {
    	res = cgi.updateTask()
    }

    if (!res) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "任务处理失败",
	        "data": "{}",
	    })
		fmt.Println("process failed, ")
		return
	}

    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": "",
    })

}

